<?php

namespace App\V3\V3_2_7\Controllers;

use App\Http\Controllers\Controller;
use App\Http\Requests\V3_2_7\VerifyIdRequest;
use App\V3\V3_2_7\Services\LocationLogService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class LocationLogController extends BaseController
{
    /**
     *日志列表
     * Author: zlf
     * Created at 2024/9/9 9:39
     * @param VerifyIdRequest $request
     * @return JsonResponse|null
     * @throws \Random\RandomException
     */
    public function logList(VerifyIdRequest $request): ?JsonResponse
    {
        $data = $request->all();
        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();
        return LocationLogService::logList($data);
    }

    /**
     *日志详情
     * Author: zlf
     * Created at 2024/9/9 9:39
     * @param VerifyIdRequest $request
     * @return JsonResponse|null
     * @throws \Random\RandomException
     */
    public function logInfo(VerifyIdRequest $request): ?JsonResponse
    {
        $data = $request->all();
        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();
        return LocationLogService::logInfo($data);
    }

    public function create(Request $request)
    {
        $data = $request->all();
        $time = time();
        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();
        $this->operateLog('添加库位日志', $data, $time);
        return Cache::lock(__CLASS__ . __FUNCTION__ . $data['user_id'], 5)
            ->get(function () use ($data, $time) {
                unset($data['id']);
                return LocationLogService::create($data, $time);
            });
    }

    public function upd(VerifyIdRequest $request)
    {
        $data = $request->all();
        $time = time();
        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();
        $this->operateLog('修改库位日志', $data, $time);
        return Cache::lock(__CLASS__ . __FUNCTION__ . $data['user_id'], 5)
            ->get(function () use ($data, $time) {
                return LocationLogService::upd($data, $time);
            });

    }

    public function del(VerifyIdRequest $request)
    {
        $data = $request->all();
        $time = time();
        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();
        $this->operateLog('删除库位日志', $data, $time);
        return Cache::lock(__CLASS__ . __FUNCTION__ . $data['user_id'], 5)
            ->get(function () use ($data, $time) {
                return LocationLogService::del($data, $time);
            });

    }

    public function getStorageAnimalList(VerifyIdRequest $request): JsonResponse
    {
        $data = $request->all();
        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();
        return $this->success(LocationLogService::getStorageAnimalList($data));
    }

}
